package com.wysql.common.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wysql.common.model.vo.SysUserVo;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;

public class UserVoUtil {
    public static SysUserVo getUser(HttpServletRequest request) {
        Enumeration<String> info = request.getHeaders("INFO");
        if (!info.hasMoreElements()) {
            return new SysUserVo();
        }

        String authorization = info.nextElement();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String decode = URLDecoder.decode(authorization, "UTF-8");
            return objectMapper.readValue(decode, SysUserVo.class);
        } catch (JsonProcessingException | UnsupportedEncodingException e) {
            e.printStackTrace();
            return new SysUserVo();
        }
    }
}
